package com.ermax.aircond.maintain.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ermax.aircond.maintain.dao.BuildingDao;
import com.ermax.aircond.maintain.domain.Building;
import com.ermax.aircond.util.dao.AbstractHibernateDaoImpl;

@Repository("buildingDao")
public class BuildingDaoImpl extends AbstractHibernateDaoImpl<Building> implements BuildingDao, java.io.Serializable {

	private static final long serialVersionUID = 1L;

	public BuildingDaoImpl() {
		setClazz(Building.class);
	}

	@Override
	public List<Building> findBuildings(String keyword) {
		Criteria criteria = this.getCurrentSession().createCriteria(Building.class);
		
		if (isNotNull(keyword)) {
			StringBuilder sbKey = new StringBuilder();
			sbKey.append("%");
			sbKey.append(keyword);
			sbKey.append("%");
			String key = sbKey.toString();

			criteria.add(Restrictions.like("buildingName", key).ignoreCase());			
			criteria.add(Restrictions.like("description", key).ignoreCase());
		}
		
		criteria.addOrder(Order.asc("buildingId"));
		criteria.addOrder(Order.asc("buildingName"));

		@SuppressWarnings("unchecked")
		List<Building> results = criteria.list();
		return results;
	}

}
